<?php 
namespace app\admin\model;
use think\Model;

class Goods extends Model
{
	protected $autoWriteTimestamp = true;

	public function getGoods()
	{
		$data = [
			'dels' => ['neq',0],
		];

		$order = [
			'id' => 'desc',
		];

	 	return $this->where($data)->order($order)->paginate(4);
	}

	// 查询一级分类下所有商品
	public function getGoodsByCategory1($category1_id)
	{
		$data = [
			'category1_id' => $category1_id,
			'dels' => ['neq',0],
		];
		$order = [
			'id' => 'desc',
		];
		return $this->where($data)->order($order)->paginate(4,false,['query' => ['id' => $category1_id]]);
	}

	// 首页展示
	public function getGoodsByPid($pid)
	{
		$data = [
			'category1_id' => $pid,
			'dels' => ['neq',0],
		];
		$order = [
			'id' => 'desc',
		];
		return $this->where($data)->order($order)->limit(4)->select();
	}

	// 查询二级分类下所有商品
	public function getGoodsByCategory2Id($category2_id)
	{
		$data = [
			'category2_id' => $category2_id,
			'dels' => ['neq',0],
		];
		$order = [
			'clicks' => 'desc',
			'id' => 'desc',
		];
		return $this->where($data)->order($order)->paginate(4);
	}

	
	// 新品
	public function getNewgoods()
	{
		$data = [
			'dels' => ['neq',0],
			'hot' => 5,
			'status' => 1,
		];
		$order = [
			'create_time' => 'desc',
			'id' => 'desc',
		];
		return $this->where($data)->order($order)->limit(10)->select();
	}

	// 特惠品
	public function getDiscount()
	{
		$data = [
			'dels' => ['neq',0],
			'hot' => 1,
			'status' => 1,
		];
		$order = [
			'create_time' => 'desc',
			'id' => 'desc',
		];
		return $this->where($data)->order($order)->limit(3)->select();
	}

	public function getStatusAttr($status)
	{
		if($status == 1) {
        $str = "<span class='label label-success radius'>上架</span>";
	    }elseif($status ==0) {
	        $str = "<span class='label label-danger radius'>下架</span>";
	    }
	    return $str;
	}


}